package org.linlinjava.litemall.db.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.linlinjava.litemall.db.dao.SearchHistoryMapper;
import org.linlinjava.litemall.db.models.SearchHistory;
import org.linlinjava.litemall.db.service.SearchHistoryService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class SearchHistoryServiceImpl extends ServiceImpl<SearchHistoryMapper, SearchHistory>
    implements SearchHistoryService {

    @Override
    public List<SearchHistory> queryByUid(Long uid) {
        QueryWrapper<SearchHistory> query = new QueryWrapper<>();
        query.eq("user_id", uid);
        query.select("DISTINCT user_id, keyword");
        return baseMapper.selectList(query);
    }

    @Override
    public void deleteByUid(Long uid) {
        QueryWrapper<SearchHistory> query = new QueryWrapper<>();
        query.eq("user_id", uid);
        baseMapper.delete(query);
    }

    @Override
    public IPage<SearchHistory> querySelective(String userId, String keyword, Integer pageNo, Integer pageSize) {
        Page<SearchHistory> page = new Page<>(pageNo, pageSize);
        QueryWrapper<SearchHistory> query = new QueryWrapper<>();
        query.eq(!StringUtils.isEmpty(userId), "user_id", userId);
        query.like(!StringUtils.isEmpty(keyword), "keyword", keyword);

        return baseMapper.selectPage(page, query);
    }
}
